<template>
  <div class="hearder-search">
    <div class="breadcrumb-container input-val-box">
      <component
          v-bind:is="item.propertyType"
          :property.sync="item"
          v-for="(item, index) in getPower"
          :key="index"
          @updateVal="updateVal"
      ></component>
      <el-button type="primary" size="small" @click="searchFn('search')"><i class="el-icon-search"></i>搜索</el-button>
      <el-button size="small" @click="searchFn('refresh')"><i class="el-icon-refresh"></i>重置刷新</el-button>
    </div>
  </div>
</template>

<script>
import dataJson from './dataJson.js'

export default {
  name: 'search',
  props: ['dataKey'],
  data() {
    return {
      optionForm: {},
      getPower: []
    }
  },
  watch: {
    getPower() {
    }
  },
  created() {
    this.getBox()
  },
  methods: {
    // 查看整合页面字段
    getBox() {
      let newLine = this.getJson(dataJson.search[this.dataKey])
      for (let i in newLine) {
        this.optionForm[i] = ''
        if (newLine[i].child) {
          for (let j in newLine[i].child) {
            if (newLine[i].child[j].name) {
              this.optionForm[newLine[i].child[j].name] = ''
            }
          }
        }
      }
      this.getPower = newLine
      // console.log('确认字段内容；', this.optionForm,newLine)
    },
    // 提交搜索内容
    searchFn(option) {
      if (option === 'refresh') {
        let newList = this.getJson(this.getPower)
        for (let i in newList) {
          newList[i].valueText = newList[i].default
          this.optionForm[i] = newList[i].default
          if (newList[i].child) {
            for (let n in newList[i].child) {
              newList[i].child[n].valueText = newList[i].child[n].default
              this.optionForm[newList[i].child[n].name] = newList[i].child[n].default
            }
          }
        }
        this.getPower = this.getJson(newList)
      }
      this.$emit('searchFn', this.getJson(this.optionForm))
    },
    // 更新值
    updateVal(option) {
      console.log('变更数据收到的数据内容：', option)
      if (option.input) {
        this.optionForm[option['input'].name] = option['input'].valueText
      } else if (option.select) {
        for (let i in option.select) {
          if(option.select[i].name){
            this.optionForm[option.select[i].name] = option.select[i].valueText
          }else{
            this.optionForm[option.select[i-1].name] = option.select[i].valueText?option.select[i].valueText:option.select[i-1].valueText
          }
        }
      }
      // console.log('最后的搜索内容：', this.optionForm)
    },
    refreshFn(){
      // this.searchFn('refresh')
      let newList = this.getJson(this.getPower)
      for (let i in newList) {
        newList[i].valueText = newList[i].default
        this.optionForm[i] = newList[i].default
        if (newList[i].child) {
          for (let n in newList[i].child) {
            newList[i].child[n].valueText = newList[i].child[n].default
            this.optionForm[newList[i].child[n].name] = newList[i].child[n].default
          }
        }
      }
      this.getPower = this.getJson(newList)
    },
    getJson(item){
      return JSON.parse(JSON.stringify(item))
    }
  },
  mounted() {
  }
}
</script>
<style>
.hearder-search .input-val-box{
  display: flex;
}
</style>

